package com.zq.it.tool;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;

public final class Constant {

	public static final String USER = "user";
	public static final String TEACHER = "teacher";
	public static final String STUDENT = "student";
	public static final String ADMIN = "admin";
	public static final String STATIC_BASE_PATH = "D:/apache/Apache24/web/video";
	public static final String STATIC_BASE_URL = "http://127.0.0.1:8088/web/video";
	public static final String ARTICALE_PATH = "D:/apache/Apache24/web/article";
	public static final String ARTICALE_URL = "http://127.0.0.1:8088/web/article";
	public static final String NETCOURSE_TYPES = "netcourseTypes";
	public static final String NETCOURSE = "netcourse";
	public static final String SECURITY_CODE = "security_code";
	public static final String MSG_SAVE_URL = "/webpage/adminpage/msg/msgContent/";
	public static final String DOWNLOAD_URL = "C:/Users/Administrator/Downloads/";
	private static final JsonConfig jsc = new JsonConfig();
	private static SimpleDateFormat sdf = null;
	
	
	public static final String ARTICLES_LIST = "articles";
	/**
	 * 声明：map用于存储用户访问数据 String： 用户账号 Class:实体类
	 * 
	 * 
	 */
	public static final Map ETYLIST_MAP = new HashMap<String, Map<Class<?>, Collection<?>>>();

	// 中文乱码解决

	public static String toUtf8String(String s) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < s.length(); i++) {
			char c = s.charAt(i);
			if (c >= 0 && c <= 255) {
				sb.append(c);
			} else {
				byte[] b;
				try {
					b = Character.toString(c).getBytes("utf-8");
				} // 返回utf-8编码
				catch (Exception ex) {
					System.out.println(ex);
					b = new byte[0];
				}
				for (int j = 0; j < b.length; j++) {
					int k = b[j];
					if (k < 0)
						k += 256;
					sb.append("%" + Integer.toHexString(k).toUpperCase());
				}
			}
		}
		return sb.toString();
	}

	public enum Regex {
		tId("^[1-9]{5,9}$"), // 教师编号5-9位数字
		tNa("^([\\u4E00-\\u9FA5\\uF900-\\uFA2D]|[0-9A-Za-z]){1,6}$"), // 1-6位字符
		// tDe("^\\S+$"), //学历查询不为空
		tCon("^(13[0-9]{9}|15[012356789][0-9]{8}|18[0256789][0-9]{8}" + // 电话11位数字
				"|147[0-9]{8}|"
				+ "\\w+((-\\w+)|(\\.\\w+))*\\@[A-Za-z0-9]+((\\.|-)[A-Za-z0-9]+)*\\.[A-Za-z0-9]+)$"), // 邮箱
				tSex("^(\\u7537|\\u5973){1}$"), // 性别
				tIdc("(?i)^\\d{17}(\\d|x)$"), // 身份证18位
				SPACE("\\s+");

		private String regex;
		Regex(String regex) {
			this.regex = regex;
		}

		@Override
		public String toString() {
			return String.valueOf(this.regex);
		}
	}
	public static Date getDateFromStr(String dateStr) {
		Date date = null;
		if (sdf == null)
			sdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			date = sdf.parse(dateStr);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return date;
	}

	public static String getStrFromDate(Date date) {
		if (sdf == null)
			sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.format(date);
	}

	public static JsonConfig getJcf(String[] fieldName) {
		JsonConfig config = new JsonConfig();
		config.setJsonPropertyFilter(new ProFilter(fieldName));
		return config;
	}

	public static JsonConfig getJcf(String[] fieldName,
			final String dateFormat) {
		JsonConfig config = new JsonConfig();
		if (fieldName != null)
			config.setJsonPropertyFilter(new ProFilter(fieldName));
		config.registerJsonValueProcessor(Date.class, new JsonValueProcessor() {
			@Override
			public Object processArrayValue(Object arg0, JsonConfig arg1) {
				// TODO Auto-generated method stub
				return process(arg0);
			}
			// 得到日期配置
			@Override
			public Object processObjectValue(String arg0, Object arg1,
					JsonConfig arg2) {
				// TODO Auto-generated method stub
				return process(arg1);
			}

			private Object process(Object value) {
				try {
					if (value instanceof Date) {
						SimpleDateFormat sdf = new SimpleDateFormat(dateFormat,
								Locale.UK);
						return sdf.format((Date) value);
					}
					return value == null ? "" : value.toString();
				} catch (Exception e) {
					return "";
				}
			}

		});

		return config;
	}

	public static JsonConfig getJcf(final String dateFormat) {

		jsc.registerJsonValueProcessor(Date.class, new JsonValueProcessor() {
			@Override
			public Object processArrayValue(Object arg0, JsonConfig arg1) {
				// TODO Auto-generated method stub
				return process(arg0);
			}
			// 得到日期配置
			@Override
			public Object processObjectValue(String arg0, Object arg1,
					JsonConfig arg2) {
				// TODO Auto-generated method stub
				return process(arg1);
			}

			private Object process(Object value) {
				try {
					if (value instanceof Date) {
						SimpleDateFormat sdf = new SimpleDateFormat(dateFormat,
								Locale.UK);
						return sdf.format((Date) value);
					}
					return value == null ? "" : value.toString();
				} catch (Exception e) {
					return "";
				}
			}

		});

		return jsc;
	}
	// 注册json Float 转换为String.
	public static JsonConfig getJcf(Class<?> c) {

		jsc.registerJsonValueProcessor(c, new JsonValueProcessor() {
			@Override
			public Object processArrayValue(Object arg0, JsonConfig arg1) {
				// TODO Auto-generated method stub
				return process(arg0);
			}
			// 得到日期配置
			@Override
			public Object processObjectValue(String arg0, Object arg1,
					JsonConfig arg2) {
				// TODO Auto-generated method stub
				return process(arg1);
			}

			private Object process(Object value) {
				return value.toString();
			}

		});

		return jsc;
	}

	public static String UTF_8 = "UTF-8";
	public static String ISO_8859_1 = "ISO-8859-1";
	public static String GBK = "GBK";
	public static String GB2312 = "GB2312";
	public static int PAGE_SIZE = 10;
	public static int PER_PAGE_COUNT = 5;
	public static String DEFAULT_ENCODING = UTF_8;

	public static String XML_CONTENT_TYPE = "text/xml";
	public static String HTML_CONTENT_TYPE = "text/html";
	public static String JSON_CONTENT_TYPE = "application/json";

	public static int MAX_USERNAME_LENGTH = 20;
	public static int MIN_USERNAME_LENGTH = 5;
	public static int MAX_PASSWORD_LENGTH = 20;
	public static int MIN_PASSWORD_LENGTH = 5;
	public static int MAX_NAME_LENGTH = 15;
	public static int MAX_BIR_LENGTH = 20;
	public static int MAX_TITLE_LENGTH = 30;
	public static int MAX_DEGREE_LENGTH = 20;
	public static int MAX_EMAIL_LENGTH = 30;
	public static int MAX_TEACHERNO_LENGTH = 20;
	public static int MAX_QUESTION_LENGTH = 50;
	public static int MAX_ANSWER_LENGTH = 50;
	public static int MAX_SNAME_LENGTH = 10;
	public static int MIN_SNAME_LENGTH = 1;
	public static final int MAX_SNO_LENGTH = 30;
	public static final int MIN_SNO_LENGTH = 5;
	public static final int IDCARDNO_LENGTH = 18;
	public static final int MAX_MOBILE_LENGTH = 11;
	public static final int MIN_MOBILE_LENGTH = 7;
	public static final int MIN_CLASS_LENGTH = 1;
	public static final int LENGTH_20 = 20;
	public static final int LENGTH_50 = 50;
	public static final int LENGTH_1 = 1;
	public static final int QTYPE_SELECT = 1;
	public static final int QTYPE_BLANK = 2;
	public static final int QTYPE_JUDGE = 3;
	public static final int QTYPE_ANSWER = 4;
	public static final long EXAM_DELAY = 3 * 60 * 1000;

	public static String ACCOUNT_IN_SESSION = "account";
	public static String Teacher_IN_SESSION = "teacher";
	public static String Student_IN_SESSION = "student";

	public static String PERMISSION_KEY = "permissionKey";

	/**
	 * 校验文件存放路径
	 * 
	 */
	public static String VALIDATE_XML = "com/zq/it/service/validate/EntityRules.xml";

	public static String PERMISSION_CONFIG = "com/zq/it/aspect/permission.properties";

	/**
	 * 头像存放路径
	 */
	public static String HEAD_PIC = "/upload/head/";

	/**
	 * 任务上传文件存放路径
	 */
	//public static String STUDENT_TASK_FILES = "/upload/studentTaskFile/";
	public static String CLASS_TASK = "D:/apache/Apache24/web/upload/studentTaskFile/classtask/";
	public static String STUDENT_TASK = "D:/apache/Apache24/web/upload/studentTaskFile/studenttask/";
	public static String STUDENT_TASK_DOWNLOAD_URL = "127.0.0.1:8088/web/upload/studentTaskFile/studenttask";
	public static String CLASS_TASK_DOWNLOAD_URL = "127.0.0.1:8088/web/upload/studentTaskFile/classtask";
	/**
	 * 任务模板上传路径
	 */
	public static String TASKMODEL = "/upload/taskModel/";
	/**
	 * 课件上传路径
	 */
	public static String COURSEWARE = "/upload/courseWare/";
	/**
	 * teacher导入模板下载
	 */
	public static String IMPORT_TEACHER_MODEL = "/download/TeacherImportModel.xls";
	/**
	 * student导入模板下载
	 */
	public static String IMPORT_STUDENT_MODEL = "/download/StudentImportModel.xls";
	/**
	 * teacher使用手册下载
	 */
	public static String TEACHER_HELP = "/download/TeacherHelp.chm";
	/**
	 * student使用手册下载
	 */
	public static String STUDENT_HELP = "/download/StudentHelp.chm";
	/**
	 * 导出试卷路径
	 */
	public static String PAPER_EXPORT = "/download/paper/";
}
